home *** CD-ROM | disk | FTP | other *** search
- @echo off
- ! uniqueName.bat Changes an existing name and makes it unique
- !
- ! uniqueName name varName
- !
- ! where 'name' is the name to be made unique (possibly including a path) and 'varName'
- ! is a string containing the name of the global variable into which the unique file name
- ! is to be returned.
- ! For example: uniqueName "bla\whatever" "storeHere"
- ! sets the variable named "storeHere" to "whatever 2". If in the folder "bla" there is
- ! already an item (folder or file) named "whatever 2", uniqueName sets storeWhere to
- ! "whatever 3", etc.
- !
- ! If the length of the item name exceeds 31 after appending the sequence number, the
- ! name is shortened.
- !
- ! If the item does not exist, uniqueName sets 'varName' to the file name contained in 'name'.
- !
- ! uniqueName calls extractName
- !
- ! Copyright © 1994 by Rainbow Hill Pty Ltd. All rights reserved.
- !
-
- ! ensure that the first two parameters are there and that the third one is not
- set doserr=13
- if not "%3 " == " " goto ERR_LBL
- if "%1 " == " " goto ERR_LBL
- if "%2 " == " " goto ERR_LBL
- onerror ERR_LBL
-
- ! separate path and item name
- call extractName "%1" uniqueName_base
- set uniqueName_folder=%WHERE%
- if "%uniqueName_base%" == "%1" goto FOLDER_CHECKED_LBL
- set uniqueName_folder=%1
- decr uniqueName_folder by "%uniqueName_base%"
- onerror SPLIT_DONE_LBL
- decr uniqueName_folder by "\"
- :SPLIT_DONE_LBL
- onerror ERR_LBL
-
- ! check that the folder exists
- set doserr=27
- if not existdir "%uniqueName_folder%" goto ERR_LBL
- :FOLDER_CHECKED_LBL
-
- ! if the item does not exist, return the name unchanged
- if exist "%1" goto EXISTS_LBL
- if existdir "%1" goto EXISTS_LBL
- set %2=%uniqueName_base%
- set doserr=0
- goto DONE_LBL
-
- :EXISTS_LBL
- onerror X_EXIST_ERR_LBL
-
- :TRY_NEWNAME_LBL
- set uniqueName_seq=1
-
- :TRY_NEWSEQ_LBL
- set doserr=0
- incr uniqueName_seq
- set uniqueName_name=%uniqueName_base% %uniqueName_seq%
- if exist "%uniqueName_folder%\%uniqueName_name%" goto TRY_NEWSEQ_LBL
- if existdir "%uniqueName_folder%\%uniqueName_name%" goto TRY_NEWSEQ_LBL
-
- ! save the name into the variable specified by the caller
- set %2=%uniqueName_name%
- set doserr=0
- goto DONE_LBL
-
- :X_EXIST_ERR_LBL
- ! if the name is too long shorten it, else abort the batch
- if not %doserr% == 26 goto ERR_LBL
- incr uniqueName_base by @
- decr uniqueName_base by 2
- goto TRY_NEWNAME_LBL
-
- :ERR_LBL
- show %doserr%
-
- :DONE_LBL
- ! remove the temporary variables
- set uniqueName_name=
- set uniqueName_base=
- set uniqueName_seq=
- set uniqueName_folder=
-